﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using mKoenig.PictureSlide.Properties;

namespace mKoenig.PictureSlide
{
  public partial class Setup : Form
  {
    Settings settings = Settings.Default;

    public Setup()
    {
      InitializeComponent();
    }
    private void Setup_Load(object sender, EventArgs e)
    {
      chkBlur.Checked = settings.BlurBackground;
      chkPan.Checked = settings.PanStretch;
      chkTime.Checked = settings.ShowActualTime;
      chkFilename.Checked = settings.ShowFileName;
      chkDebug.Checked = settings.ShowDebug;
      chkStartRandom.Checked = settings.StartRandom;

      radRandom.Checked = (settings.PictureMode == "random");
      radSequence.Checked = (settings.PictureMode == "sequence");

      lblPath.Text = Settings.Default.PicturePath;

      numTime.Value = (decimal)Settings.Default.TimeNextPicture;
      trkTransition.Value = (int)Settings.Default.TransitionTime;
      lblTransitionTime.Text = trkTransition.Value.ToString() + " ms";

      colorDialog1.Color = settings.TextBackColor;
      trkAlpha.Value = settings.TextAlpha;

      for (int i = 0; i < Render.Count; i++)
      {
        checkedListBox1.Items.Add(Render.RenderName[i], TransitionEnabler.isEnabled(i));
      }

      this.BringToFront();
    }

    private void btnClose_Click(object sender, EventArgs e)
    {
      this.Close();
    }
    private void btnSave_Click(object sender, EventArgs e)
    {
      settings.PictureMode = "random";
      if (radSequence.Checked) settings.PictureMode = "sequence";

      settings.Save();
      this.Close();
    }

    // bool
    private void chkPan_CheckedChanged(object sender, EventArgs e)
    {
      settings.PanStretch = chkPan.Checked;
    }
    private void chkBlur_CheckedChanged(object sender, EventArgs e)
    {
      settings.BlurBackground = chkBlur.Checked;
    }
    private void chkTime_CheckedChanged(object sender, EventArgs e)
    {
      settings.ShowActualTime = chkTime.Checked;
    }
    private void chkFilename_CheckedChanged(object sender, EventArgs e)
    {
      settings.ShowFileName = chkFilename.Checked;
    }
    private void chkDebug_CheckedChanged(object sender, EventArgs e)
    {
      settings.ShowDebug = chkDebug.Checked;
    }
    private void chkStartRandom_CheckedChanged(object sender, EventArgs e)
    {
      settings.StartRandom = chkStartRandom.Checked;
    }
    // browse
    private void btnBrowsePath_Click(object sender, EventArgs e)
    {
      FolderBrowserDialog dialog = new FolderBrowserDialog();
      dialog.SelectedPath = Settings.Default.PicturePath;

      DialogResult res = dialog.ShowDialog();
      if (res == DialogResult.OK)
      {
        settings.PicturePath = dialog.SelectedPath;
        lblPath.Text = dialog.SelectedPath;
      }
    }
    // numeric
    private void numTime_ValueChanged(object sender, EventArgs e)
    {
      settings.TimeNextPicture = (int)numTime.Value;
    }

    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
      int x = checkedListBox1.SelectedIndex;
      if (x >= 0)
      {
        DataTable tab = settings.DisableTransition;

        tab.Rows[0][x] = "false";
        bool result = checkedListBox1.GetItemChecked(x);
        if (result) tab.Rows[0][x] = "true";

        settings.DisableTransition = tab;
      }
    }

    private void button1_Click(object sender, EventArgs e)
    {
      colorDialog1.Color = settings.TextBackColor;

      DialogResult res = colorDialog1.ShowDialog();
      if (res == System.Windows.Forms.DialogResult.OK)
      {
        settings.TextBackColor = colorDialog1.Color;
        UpdateColor(settings.TextFrontColor, settings.TextBackColor);
      }
    }

    private void UpdateColor(Color SelectedColor, Color BackColor)
    {
      Application.DoEvents();

      Graphics g1 = Graphics.FromHwnd(lblColor.Handle);

      string text = "this shows how it looks";

      SolidBrush drawBrush = new SolidBrush(SelectedColor);

      SizeF aSize1 = g1.MeasureString(text, new Font("Arial", 10.0f));
      Rectangle rect1 = new Rectangle(3, 12, (int)aSize1.Width, (int)aSize1.Height);

      g1.FillRectangle(new SolidBrush(Color.FromArgb(255, Color.White.R, Color.White.G, Color.White.B)), rect1);
      g1.FillRectangle(new SolidBrush(Color.FromArgb(trkAlpha.Value, BackColor.R, BackColor.G, BackColor.B)), rect1);

      g1.DrawString(text,
                                new Font("Arial", 10.0f),
                                drawBrush,
                                new Point(3, 12));
    }

    private void trkTransition_ValueChanged(object sender, EventArgs e)
    {
      settings.TransitionTime = (float)trkTransition.Value;
      lblTransitionTime.Text = trkTransition.Value.ToString() + " ms";
    }

    private void tabPage4_Paint(object sender, PaintEventArgs e)
    {

    }
    private void trkAlpha_ValueChanged(object sender, EventArgs e)
    {
      UpdateColor(settings.TextFrontColor, settings.TextBackColor);
      settings.TextAlpha = trkAlpha.Value;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
      timer1.Enabled = false;
      UpdateColor(settings.TextFrontColor, settings.TextBackColor);
    }

    private void tabPage4_Enter(object sender, EventArgs e)
    {
      timer1.Enabled = true;
    }

    private void button2_Click(object sender, EventArgs e)
    {
      colorDialog1.Color = settings.TextFrontColor;

      DialogResult res = colorDialog1.ShowDialog();
      if (res == System.Windows.Forms.DialogResult.OK)
      {
        settings.TextFrontColor = colorDialog1.Color;
        UpdateColor(settings.TextFrontColor, settings.TextBackColor);
      }
    }
  }
}
